<?php

Solar::loadClass('Solar_Base');

/**
 * @todo make this Domain51_Dispatcher to replace Solar_Controller_Front
 */
class Domain51_Dispatcher extends Solar_Base
{
    public function __construct($config = null) {
        parent::__construct($config);
        
        $this->_locators = array('PageType', 'Model');
    }
    
    public function fetch($spec = null) {
        $uri = Solar::factory('Solar_Uri_Action');
        
                // default to current URI
        $uri = Solar::factory('Solar_Uri_Action');
        
        // override current URI with user spec
        if (is_string($spec)) {
            $uri->set($spec);
        }
        
        // does the page map to a known class?
        $dispatchable = false;
        
        foreach ($this->_locators as $locator_name) {
            $locator_class = 'Controller_' . $locator_name . '_Locator';
            $locator = Solar::factory($locator_class);
            $dispatchable = $locator->locate($uri);
            if ($dispatchable !== false) {
                break;
            }
        }
        
        // did we find the page class?
        if (! $dispatchable) {
            return htmlspecialchars("404: Page '$page' unknown.");
        }
        
        return $dispatchable->dispatch($uri);
    }
    
    public function display($spec = null) {
        echo $this->fetch($spec);
    }
}

?>